﻿using System;
using Newtonsoft.Json.Linq;
using RestSharp;
using Spotted.Data.Args;
using Spotted.Data.Result;

namespace Spotted.Data.Repositories.Impl
{
    public class RestGoogleMapsRepository : IMapsRepository
    {
        private const string CITY_TYPE_KEY = "locality";
        private const string STATE_TYPE_KEY = "administrative_area_level_1";

        #region Implementation of IMapsRepository

        /// <summary>
        /// Return address information for a given point
        /// </summary>
        /// <param name="latitude">The latitude</param>
        /// <param name="longitude">The longitude</param>
        /// <returns></returns>
        public void GetMapAddressForCoordinate(double latitude, double longitude)
        {
            RestClient client = new RestClient("http://maps.googleapis.com/maps/api/");
            RestRequest request = new RestRequest("geocode/json", Method.GET);
            request.AddParameter("latlng", latitude + "," + longitude);
            request.AddParameter("sensor", "true");

            client.ExecuteAsync(request, (response, handler) =>
                                             {
                                                 JObject jsonObject = JObject.Parse(response.Content);
                                                 JArray jsonArray = JArray.Parse(jsonObject["results"].First["address_components"].ToString());
                                                 MapResult result = new MapResult
                                                                        {
                                                                            
                                                                        };

                                                 if (GetMapAddressForCoordinateCompleted != null)
                                                     GetMapAddressForCoordinateCompleted(this, new MapAddressForCoordinateCompletedEventArgs(result));
                                             });
        }

        public event EventHandler<MapAddressForCoordinateCompletedEventArgs> GetMapAddressForCoordinateCompleted;

        #endregion

        private string GetCity(IJEnumerable<JToken> collection)
        {
            return string.Empty;
        }
    }
}
